package Bean;

import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author 黄忠
 * 创建于2019-06-01
 */

@SuppressWarnings("unused")
public class UserBean {
    private String xing;
    private String ming;
    private String username;
    private String password;

    public String getTX(){
        String tx="tx.jpg";
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            ResultSet rset = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            rset=stmt.executeQuery("SELECT * FROM `localInfor` WHERE `username`='"+username+"';");
            if(rset.next()){
                tx=rset.getString("tx");
                if(!tx.equals("tx.jpg")){
                    tx=username+"/"+tx;
                }
            }
            stmt.close();
            conn.close();
            context.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return tx;
    }

    public void setTX(String tx){
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            stmt.executeUpdate("UPDATE `localInfor` SET `tx`='"+tx+"' WHERE `username`='"+username+"';");
            stmt.close();
            conn.close();
            context.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isValid(){
        boolean isValid=false;
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            ResultSet rset = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            rset=stmt.executeQuery("SELECT * FROM `localInfor` WHERE `username`='"+username+"';");
            isValid=rset.next();
            rset.close();
            stmt.close();
            conn.close();
            context.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return isValid;
    }

    public boolean isBeBind(String dgut_username,boolean loginBind){
        boolean isBeBind=false;
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            ResultSet rset = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            rset=stmt.executeQuery("SELECT * FROM `localInfor` WHERE `dgut_username`='"+dgut_username+"';");
            if(rset.next()){
                isBeBind=true;
                if(loginBind) {
                    username = rset.getString("username");
                    password = rset.getString("password");
                }
            }
            rset.close();
            stmt.close();
            conn.close();
            context.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return isBeBind;
    }

    public boolean check(){
        boolean check=false;
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            ResultSet rset = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            rset=stmt.executeQuery("SELECT * FROM `localInfor` WHERE `username`='"+username+"' AND `password`='"+password+"';");
            check=rset.next();
            rset.close();
            stmt.close();
            conn.close();
            context.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return check;
    }

    public void save(){
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            stmt.executeUpdate("UPDATE `localInfor` SET `xing`='"+xing+"',`ming`='"+ming+"' WHERE `username`='"+username+"';");
            stmt.close();
            conn.close();
            context.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void bindDgut(String dgut_username){
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            stmt.executeUpdate("UPDATE `localInfor` SET `dgut_username`='"+dgut_username+"' WHERE `username`='"+username+"';");
            stmt.close();
            conn.close();
            context.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getDgut(){
        String dgut_username="";
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            ResultSet rset = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            rset=stmt.executeQuery("SELECT * FROM `localInfor` WHERE `username`='"+username+"';");
            if(rset.next()){
                dgut_username=rset.getString("dgut_username");
                if(dgut_username==null) dgut_username="";
            }
            rset.close();
            stmt.close();
            conn.close();
            context.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return dgut_username;
    }

    public boolean signupLocal(){
        boolean signupLocal=false;
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            stmt.executeUpdate("INSERT INTO `localInfor` (`username`,`password`,`xing`,`ming`) VALUES ('"+username+"','"+password+"','"+xing+"','"+ming+"');" );
            signupLocal=true;
            stmt.close();
            conn.close();
            context.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return signupLocal;
    }

    public boolean signupDgut(String username,String name,String group,String faculty_title){
        boolean signupDgut=false;
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            stmt.executeUpdate("INSERT INTO `dgutInfor` (`username`,`name`,`group`,`faculty_title`) VALUES ('"+username+"','"+name+"','"+name+"','"+faculty_title+"');" );
            stmt.close();
            conn.close();
            context.close();
            signupDgut=true;
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return signupDgut;
    }

    public boolean login(){
        boolean login=false;
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            ResultSet rset = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            rset=stmt.executeQuery("SELECT * FROM `localInfor` WHERE `username`='"+username+"' AND `password`='"+password+"';");
            login=rset.next();
            if(login){
                xing=rset.getString("xing");
                ming=rset.getString("ming");
            }
            rset.close();
            stmt.close();
            conn.close();
            context.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return login;
    }

    public UserBean(String xing, String ming, String username, String password) {
        this.xing = xing;
        this.ming = ming;
        this.username = username;
        this.password = password;
    }

    public UserBean() {
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            stmt.executeUpdate("CREATE TABLE IF NOT EXISTS `dgutInfor` (\n" +
                    "      `username` char(12) NOT NULL UNIQUE,\n" +
                    "      `name` varchar(11) NOT NULL,\n" +
                    "      `group` varchar(11) NOT NULL,\n" +
                    "      `faculty_title` varchar(30) NOT NULL,\n" +
                    "       PRIMARY KEY (`username`)\n" +
                    "    );");
            stmt.executeUpdate("CREATE TABLE IF NOT EXISTS `localInfor` (\n" +
                    "      `username` char(30) NOT NULL UNIQUE,\n" +
                    "      `password` varchar(30) NOT NULL,\n" +
                    "      `xing` varchar(11) NOT NULL,\n" +
                    "      `ming` varchar(11) NOT NULL,\n" +
                    "      `tx` varchar(30) NOT NULL DEFAULT 'tx.jpg',\n" +
                    "      `dgut_username` char(12),\n" +
                    "       PRIMARY KEY (`username`),\n" +
                    "      FOREIGN KEY (`dgut_username`) REFERENCES `dgutInfor`(`username`)\n" +
                    "    );");
            stmt.close();
            conn.close();
            context.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    public String getXing() {
        return xing;
    }

    public void setXing(String xing) {
        this.xing = xing;
    }

    public String getMing() {
        return ming;
    }

    public void setMing(String ming) {
        this.ming = ming;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}
